package suanfa;

public class shunshizhen {
	
	private static final int INIT_RIGHT_LINE = 0;
	private static final int INIT_RIGHT_COLUMN = -1;
	private int[][] array;
	private int line;
	private int column;
	private int count;
	private int value = 1;
	private int length;
	
	public shunshizhen(int line, int column){
		array = new int[line][column];
		this.line = line;
		this.column = column;
		count = line * column;
		length = String.valueOf(count).length();
		right(INIT_RIGHT_LINE,INIT_RIGHT_COLUMN);
	}
	
	public static void main(String[] args){
		shunshizhen s = new shunshizhen(121,121);
		s.print();
		
	}
	
	public void print(){
		for(int i=0;i<line;i++){
			for(int j=0;j<column;j++){
				System.out.print( String2Length(array[i][j])+ " ");
			}
			System.out.println();
		}
	}
	
	private void right(int l,int c){
		if(value <= count) {
			while(c < column-1 && array[l][c+1] ==0) {
				c++;
				array[l][c] = value++;
			}
				down(l,c);
			
		}else {
			return;
		}
	}

	private void down(int l, int c) {
		if(value <= count) {
			while(l< line-1 && array[l+1][c] == 0){
				l++;
				array[l][c] = value++;
			}
			left(l,c);
		}else {
			return;
		}	
	}

	private void left(int l, int c) {
		if(value <= count) {
			while(c > 0 && array[l][c-1] == 0){
				c--;
				array[l][c] = value++;
			}
			up(l,c);
		}else {
			return;
		}
	}

	private void up(int l, int c) {
		if(value <= count) {
			while(l > 0 && array[l-1][c] == 0){
				l--;
				array[l][c] = value++;
			}
			right(l,c);
		}else {
			return;
		}
	}
	
	private String String2Length(int i){
		int tempLength = String.valueOf(i).length();
		StringBuilder sb = new StringBuilder();
		sb.append(i);
		for(int j=1;j<=(length - tempLength);j++){
			sb.insert(0, "0");
		}
		return sb.toString();
	}
}


